<template></template>
<script>
import { Toast } from "vant";
import { login } from "@/https/api";
export default {
    name: "login",
    data() {
        return {
            // userInfo: {
            //     mobile: "15695713828",
            // },
            userInfo: "",
        };
    },
    created() {
        Toast.loading({
            duration: 0,
            forbidClick: true,
            message: "登录中...",
        });
        // 初始化jssdk
        if (CP && typeof CP.ready === "function") {
            CP.ready({
                debug: false, //可选，是否是开启调试模式, 默认false
                es6Require: true, //可选，是否需要加载es6库，默认false
                ready: (client_info) => {
                    CP.isLogin({
                        completed: (data) => {
                            if (data.is_login == 1) {
                                this.getUserInfo();
                            } else {
                                CP.showLogin({
                                    completed: (data) => {
                                        if (data.is_login == "0") {
                                            CP.closePage();
                                        } else {
                                            this.getUserInfo();
                                        }
                                    },
                                });
                            }
                        },
                        cpIncompatible: () => {
                            CP.closePage();
                        },
                    });
                },
                error: async (error) => {
                    if (localStorage.getItem("token")) {
                        localStorage.removeItem("token");
                        CP.closePage();
                        return false;
                    }
                    // let url = await this.getAppUrl();
                    // if (url) {
                    //     window.location.href = url;
                    // }
                },
            });
        }
    },
    mounted() {
        // this.login();
    },
    methods: {
        // 获取app下载地址
        // async getAppUrl() {
        //     return new Promise(async (resolve) => {
        //         let res = await index.getAppUrl({
        //             h5Url: "https://app.tmuyun.com/xjz/xianju-h5/home",
        //         });
        //         resolve(res.data);
        //     });
        // },
        // 获取用户信息
        async getUserInfo() {
            CP.getCustomerInfo({
                completed: (data) => {
                    console.log(data);
                    this.userInfo = data;
                    this.login();
                },
            });
        },
        async login() {
            let res = await login({
                phone: this.userInfo.mobile,
            });
            Toast.clear();
            if (res.code == 1) {
                localStorage.setItem("userInfo", JSON.stringify(res.data));
                localStorage.setItem("token", res.data.token);
                this.$router.replace("/index");
            } else {
                localStorage.removeItem("userInfo");
                localStorage.removeItem("token");
                Toast(res.msg);
            }
        },
    },
};
</script>
<style scoped></style>
